package com.example.myimageshare.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myimageshare.R;
import com.example.myimageshare.Utils.DateUtil;
import com.example.myimageshare.Utils.MyTextUtil;
import com.example.myimageshare.View.PhotoViewActivity;
import com.example.myimageshare.domain.User;
import com.example.myimageshare.domain.UserArt;
import com.example.myimageshare.entity.PhotoItem;
import com.zhihu.matisse.listener.OnFragmentInteractionListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DeleteAdapter extends RecyclerView.Adapter<DeleteAdapter.ViewHolder>{
    public static final String TAG = DeleteAdapter.class.getSimpleName();
    private List<UserArt> users = new ArrayList<>();
    private Map<UserArt, Boolean> map = new HashMap<>();
    private List<UserArt> userArtsSelected = new ArrayList<>(); //已选
    private Context context;
    private int resourceId;
    private int position;
    private boolean isDelete = false;
    private boolean isSelect = true;


    public DeleteAdapter(List<UserArt> users){
        this.users = users;
    }

    public DeleteAdapter(Context context, int resourceId, List<UserArt> users){
        this.context = context;
        this.resourceId = resourceId;
        this.users = users;
        if(users!=null) {
            for (UserArt u : users) {
                map.put(u, false);
            }
        }

    }

    @NonNull
    @Override
    public DeleteAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(
                resourceId,parent,false);
        final DeleteAdapter.ViewHolder viewHolder = new DeleteAdapter.ViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final DeleteAdapter.ViewHolder holder, final int position) {
        final UserArt user = users.get(position);
        map.put(user,false);
        //不设置viewHoder的Tag，避免与Glide冲突
        this.position = position;

//        if (users.get(position).isSelected()) {
//            checkBox.setButtonDrawable(R.drawable.ic_selected);
//            coverView.setVisibility(View.VISIBLE);
//        } else {
//            coverView.setVisibility(View.GONE);
//            checkBox.setButtonDrawable(R.drawable.ic_no_select);
//        }

        if(user.getUsername() != null){
            holder.tv_name.setText(user.getUsername());
        }
        if(user.getEssay() != null){
            String str = MyTextUtil.Cut(user.getEssay());
            Log.d("DeleteAdapter","strE:"+ str);
            holder.tv_artE.setText(str + "");
        }
        if(user.getUrls().size()!=0){
            Uri uri = Uri.parse(user.getUrls().get(0));
            Log.d("DeleteAdapter","imgUrl: "+ uri);

            Glide.with(context)
                    .load(uri)
                    .thumbnail(0.1f)
                    .into(holder.iv_art);
        }

        if(user.getCreatedAt()!=null){
            holder.tv_dateTime.setText(user.getCreatedAt());
        }
        //点击显示图片事件
        View.OnClickListener showImg = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, PhotoViewActivity.class);
                Log.d("DeleteAdapter",users.get(position).getUrls().toString());
                Bundle userArtBundle = new Bundle();
                userArtBundle.putSerializable("userArt", users.get(position));
                intent.putExtras(userArtBundle);
                intent.putExtra("currentPosition",1);

                context.startActivity(intent);
            }
        };
        //点击选中图片事件
        View.OnClickListener chooseImg = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(map.get(users.get(position)).booleanValue()) {
                    //图片已选择，点击后设置未选择
                    holder.checkBox.setButtonDrawable(R.drawable.ic_no_select);
                    holder.coverView.setVisibility(View.GONE);
                    //移除已选中的数据
                    map.put(users.get(position),false);
                    userArtsSelected.remove(users.get(position));
                    //回调
                    if (mItemSelectChanged != null) {
                        mItemSelectChanged.changeSelectedCount(userArtsSelected);
                    }
                }else{
                    //图片未选择，点击后设置已选择
                    holder.coverView.setVisibility(View.VISIBLE);
                    holder.checkBox.setButtonDrawable(R.drawable.ic_selected);
                    //添加刚选中的数据
                    map.put(users.get(position),true);
                    userArtsSelected.add(users.get(position));
                    //回调
                    if (mItemSelectChanged != null) {
                        mItemSelectChanged.changeSelectedCount(userArtsSelected);
                    }
                }
            }
        };
        //
        if(isDelete){
            holder.checkBox.setButtonDrawable(R.drawable.ic_no_select);
            holder.checkBox.setVisibility(View.VISIBLE);
            Log.d(TAG,"进入判断isDelete");
            holder.iv_art.setOnClickListener(null);
            holder.iv_art.setOnClickListener(chooseImg);
        }else {
            holder.checkBox.setVisibility(View.GONE);
            holder.coverView.setVisibility(View.GONE);
            holder.iv_art.setOnClickListener(null);
            holder.iv_art.setOnClickListener(showImg);
        }

    }



    public void setIsDelete(boolean isDelete){
        Log.d(TAG,"显示可选");
        this.isDelete = isDelete;

    }

    private DeleteAdapter.OnItemSelectChanged mItemSelectChanged;
    // 暴露接口给外部
    public void setOnItemSelectChanged(DeleteAdapter.OnItemSelectChanged itemSelectChanged) {
        this.mItemSelectChanged = itemSelectChanged;
    }

    public void release() {
        userArtsSelected.clear();
    }

    public interface OnItemSelectChanged {
        void changeSelectedCount(List<UserArt> selectedImages);
    }

    public List<UserArt> getUserArtsSelected(){
        return userArtsSelected;
    }


    @Override
    public int getItemCount() {
        return users != null ? users.size() :0;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView iv_art;
        TextView tv_name;
        ImageView iv_likes;
        TextView tv_dateTime;
        TextView tv_artE;
        CheckBox checkBox;
        View coverView;


        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_art = itemView.findViewById(R.id.iv_image_delete);
            tv_name = itemView.findViewById(R.id.tv_username_delete);
            iv_likes = itemView.findViewById(R.id.iv_likes_delete);
            tv_dateTime = itemView.findViewById(R.id.tv_DateTime_delete);
            tv_artE = itemView.findViewById(R.id.tv_artE_delete);
            checkBox = itemView.findViewById(R.id.bt_is_selected);
            coverView = itemView.findViewById(R.id.image_cover_delete);

        }


    }
}
